﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cost.Models;
using NBlock.MVCControl;

namespace Cost.Controllers
{
    public class DishSignViewController : Controller
    {
        private readonly ICostRepository<DishDelivery> dishdeliveryRepository;
        private readonly ICostRepository<DishDeliveryItem> dishdeliveryitemRepository;

        public DishSignViewController(ICostRepository<DishDelivery> dishdeliveryRepository,
            ICostRepository<DishDeliveryItem> dishdeliveryitemRepository)
        {
            this.dishdeliveryRepository = dishdeliveryRepository;
            this.dishdeliveryitemRepository = dishdeliveryitemRepository;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult List(nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = dishdeliveryRepository.GetPageable(pager, m => m.State == "Sign", where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        public ActionResult Edit(string Id)
        {
            var data = dishdeliveryRepository.Find(Id) ?? new DishDelivery();
            if (string.IsNullOrEmpty(data.CreateUserId))
            {
                data.Date = DateTime.Now;
            }
            return View(data);
        }

        [HttpPost]
        public JsonResult EditList(string parentId, nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = dishdeliveryitemRepository.GetPageable(pager, d => d.OrderId == parentId, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }
    }
}
